/**
 * Created by wangjian on 17/5/3.
 */
myApp.controller('AddPlanProjectCtrl', function ($scope, $uibModal, $uibModalInstance, info, $http, ngDialog) {
    $scope.info = info;
    //项目信息
    $scope.info.planInfo = {};
    $scope.info.projectType = '0';
    //选中的trunk name
    $scope.info.trunkName = "";
    //可选择的主干工程列表
    $scope.info.showTrunkList = [];
    //要保存的工程信息
    $scope.info.projectInfo = {};
    $scope.info.svn_form = "";
    $scope.info.msg = '';

    //选择已有工程时参数
    $scope.info.trunkInfo = {};
    //工程列表
    $scope.info.trunkList = [];
    $scope.info.projectList = [];

    $scope.proTypeChange = function () {
        if ($scope.info.projectType == '0') {
            $scope.info.trunkName = "";
            $scope.info.projectInfo = {};
            $scope.info.svn_form = "";
            $scope.info.msg = '';
        } else if ($scope.info.projectType == '1') {
            $scope.getBeetleTrunkList();
        }
    };

    $scope.proTypeChange();

    //获取beetle管理的主干列表
    $scope.getBeetleTrunkList = function () {
        $scope.info.trunkList = [];
        doBeetlePost($http, $uibModal, java_url + '/beetle/project/projectInfoList', {
            tokenId: getTokenId(),
            onlyTrunk: true,
            isShowJar: true
        }, function (data) {
            if (data.code != '0') {
                alert("获取工程信息失败,desc=" + data.desc);
                return;
            }
            angular.forEach(data.data.list, function (trunk) {
                var isExist = 0;
                angular.forEach($scope.info.planInfo.projects, function (pro) {
                    if (trunk.projectName == pro.trunk_name) {
                        isExist = 1;
                        return false;
                    }
                });
                if (isExist == 0) {
                    $scope.info.trunkList.push(trunk);
                }
            });
            $scope.info.trunkInfo = $scope.info.trunkList[0];
            $scope.getBeetleProjectListByTrunk();
        })
    };

    //根据主干获取beetle管理的工程列表
    $scope.getBeetleProjectListByTrunk = function () {
        $scope.info.projectList = [];
        doBeetlePost($http, $uibModal, java_url + '/beetle/project/projectListByTrunk', {
            tokenId: getTokenId(),
            trunkName: $scope.info.trunkInfo.projectName
        }, function (data) {
            if (data.code != '0') {
                alert("获取工程信息失败,desc=" + data.desc);
                return;
            }
            $scope.info.projectList = data.data.list;
            $scope.info.projectInfo = $scope.info.projectList[0];
        })
    };

    //获取项目信息
    $scope.getPlanInfo = function () {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/project_plan/getPlanList?hasProList=true&isShowOnline=true&token_id=' + getTokenId() + '&plan_id=' + $scope.info.planId, function (data) {
            $scope.info.planInfo = data.data[0];
            $scope.getTrunkProjectList();
        })
    };

    $scope.getPlanInfo();

    $scope.getTrunkProjectList = function () {
        doBeetleGet($http, $uibModal, beetle_plan_url + '/project/getTrunkProjectList?token_id=' + getTokenId(), function (data) {
            var list = data.data.trunkProject;
            angular.forEach(data.data.jarProject, function (jar) {
                list.push(jar);
            });
            angular.forEach(list, function (trunk) {
                var isExist = 0;
                angular.forEach($scope.info.planInfo.projects, function (pro) {
                    if (trunk.project_name == pro.trunk_name) {
                        isExist = 1;
                        return false;
                    }
                });
                if (isExist == 0) {
                    $scope.info.showTrunkList.push(trunk);
                }
            });
        })
    };

    $scope.getPlanProjectDefaultName = function () {
        var list = [];
        $scope.info.svn_form = "trunk";
        if ($scope.info.trunkName != "") {
            list.push($scope.info.trunkName)
        } else {
            return false;
        }
        doBeetleGet($http, $uibModal, beetle_plan_url + '/project_plan/getPlanProjectDefaultName?token_id=' + getTokenId() + '&plan_code=' + $scope.info.planInfo.plan_code + '&trunk_list=' + JSON.stringify(list), function (data) {
            $scope.info.projectInfo = data.data[0];
        });
    };
    $scope.updateBranch_num = function () {
        var branch_num = $scope.info.projectInfo.branch_num;
        var t = /^(\d{1,4}){1}(\.\d{1,4}){2}$/;
        if (branch_num.match(t)) {
            $scope.info.branch_numLock = false;
            $scope.info.msg = '';
        } else {
            $scope.info.branch_numLock = true;
            $scope.info.msg = '请输入正确的版本号！格式：0.0.0';
        }
    };

    $scope.save = function () {
        if(info.projectType == '0'){
            if ($scope.info.trunkName == '') {
                alert('请选择工程');
                return false;
            }
            if ($scope.info.svn_form == '') {
                alert('请选择工程来源!');
                return false;
            }
            $scope.info.project_name = $scope.info.projectInfo.trunk_name + '-' + $scope.info.projectInfo.branch_num + '.' + $scope.info.planInfo.plan_code;

            doBeetleGet($http, $uibModal, beetle_plan_url + '/project_plan/addPlanProject?project_name=' + $scope.info.project_name + '&plan_id=' + $scope.info.planInfo.plan_id + '&branchNum=' + $scope.info.projectInfo.branch_num + '&trunk_name=' + $scope.info.trunkName + '&token_id=' + getTokenId() + '&fromProject=' + $scope.info.svn_form, function (data) {
                if (data.success) {
                    $uibModalInstance.dismiss('save');
                } else {
                    openDialogAlert(ngDialog, $scope, data.msg);
                }
            });
        }else if(info.projectType == '1'){
            doBeetleGet($http, $uibModal, beetle_plan_url + '/project_plan/addPlanProjectExist?project_name=' + $scope.info.projectInfo.projectName + '&plan_id=' + $scope.info.planInfo.plan_id + '&trunk_name=' + $scope.info.trunkInfo.projectName + '&token_id=' + getTokenId(), function (data) {
                if (data.success) {
                    $uibModalInstance.dismiss('save');
                } else {
                    openDialogAlert(ngDialog, $scope, data.msg);
                }
            });
        }
    };

    $scope.cancel = function () {
        // 关闭弹出框
        $uibModalInstance.close('cancel');
    };
});